layui.config({
	base: '/js/module/'
}).extend({
	dialog: 'dialog',
});

layui.use(['table','form', 'jquery', 'laydate', 'layer', 'laypage', 'dialog', 'element'], function() {
	var form = layui.form,
		layer = layui.layer,
		table = layui.table,
		$ = layui.jquery,
		dialog = layui.dialog;
	//获取当前iframe的name值
	var iframeObj = $(window.frameElement).attr('name');
	//全选
	form.on('checkbox(allChoose)', function(data) {
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
		child.each(function(index, item) {
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});
	//渲染表单
	form.render();	
	//顶部添加
	$('.addBtn').click(function() {
		var url=$(this).attr('data-url');
		//将iframeObj传递给父级窗口,执行操作完成刷新
		page("添加", url, iframeObj, w = "80%", h = "620px");
		return false;

	}).mouseenter(function() {

		dialog.tips('添加', '.addBtn');

	});

    $('.assign').click(function() {
        var checkStatus = table.checkStatus('test'),data = checkStatus.data;
        if(data.length<1){
            layer.msg("请选择一条记录")
            return false;
        }
        if(data.length>1){
            layer.msg("只能选择一条记录")
            return false;
        }
        var url=$(this).attr('data-url');
        url = url+"?id="+data[0].id;
        //将iframeObj传递给父级窗口,执行操作完成刷新
        page("设置", url, iframeObj, w = "700px", h = "620px");
        return false;

    }).mouseenter(function() {

        dialog.tips('设置', '.assign');

    });
	//顶部排序
	$('.listOrderBtn').click(function() {
        var checkStatus = table.checkStatus('test'),data = checkStatus.data;
        if(data.length<1){
            layer.msg("请选择一条记录")
            return false;
        }
        if(data.length>1){
            layer.msg("只能选择一条记录")
            return false;
        }
        //var url = $(this).attr('data-url');
        var status = $("#status").val();

       	if(status == 1){
            var taskId = data[0].id;

            layer.open({
                type: 2,
                title:'审批记录',
                content: '/examine?taskId='+taskId,
                area: ['60%', '60%'],
                success: function(layero, index){
                }
            });
        }else if(status == 0){
       		layer.msg("已经开始审批 ！！");
		}else {
            layer.msg("审批已经结束！！");

		}

		return false;

	}).mouseenter(function() {

		dialog.tips('审批', '.listOrderBtn');

	})
	/*修改记录*/
	$('.updateBtn').click(function () {
		var checkStatus = table.checkStatus('test'),data = checkStatus.data;
		if(data.length<1){
			layer.msg("请选择一条记录")
			return false;
		}
		if(data.length>1){
			layer.msg("只能选择一条记录")
			return false;
		}
		var id = data[0].id;
		var url=$(this).attr('data-url')+'?id='+id;
		layer.open({
			type: 2,
			title:'修改记录',
			content: url,
			area: ['60%', '60%'],
			success: function(layero, index){
			}
		});
		// 注意一定要加 将页面展示
		return false;
	}).mouseenter(function() {
		dialog.tips('修改', '.updateBtn');
	})



	$('.viewBtn').click(function () {
		var checkStatus = table.checkStatus('test'),data = checkStatus.data;
		if(data.length<1){
			layer.msg("请选择一条记录")
			return false;
		}
		if(data.length>1){
			layer.msg("只能选择一条记录")
			return false;
		}
		var url=$(this).attr('data-url')+'?executionid='+data[0].processInstanceId;
		layer.open({
			type: 2,
			title:'查看记录',
			content: url,
			area: ['90%', '90%'],
			success: function(layero, index){
			}
		});
		// 注意一定要加 将页面展示
		return false;
	}).mouseenter(function() {
		dialog.tips('查看', '.viewBtn');
	});

	$('.viewBtn2').click(function () {
		var checkStatus = table.checkStatus('test4'),data = checkStatus.data;
		if(data.length<1){
			layer.msg("请选择一条记录")
			return false;
		}
		if(data.length>1){
			layer.msg("只能选择一条记录")
			return false;
		}
		var url=$(this).attr('data-url')+'?id='+data[0].id;
		layer.open({
			type: 2,
			title:'查看记录',
			content: url,
			area: ['90%', '90%'],
			success: function(layero, index){
			}
		});
		// 注意一定要加 将页面展示
		return false;
	}).mouseenter(function() {
		dialog.tips('查看', '.viewBtn2');
	});



	$('.dealBtn').click(function () {
		var checkStatus = table.checkStatus('test'),data = checkStatus.data;
		if(data.length<1){
			layer.msg("请选择一条记录")
			return false;
		}
		if(data.length>1){
			layer.msg("只能选择一条记录")
			return false;
		}
		console.log(data[0]);
		var url=$(this).attr('data-url')+'?taskId='+data[0].taskId;
		layer.open({
			type: 2,
			title:'处理记录',
			content: url,
			area: ['60%', '60%'],
			success: function(layero, index){
			}
		});
		// 注意一定要加 将页面展示
		return false;
	}).mouseenter(function() {
		dialog.tips('处理', '.dealBtn');
	});

	$('.dealBtn2').click(function () {
		var checkStatus = table.checkStatus('test2'),data = checkStatus.data;
		if(data.length<1){
			layer.msg("请选择一条记录")
			return false;
		}
		if(data.length>1){
			layer.msg("只能选择一条记录")
			return false;
		}
		console.log(data[0]);
		var url=$(this).attr('data-url')+'?taskId='+data[0].taskId;
		layer.open({
			type: 2,
			title:'处理记录',
			content: url,
			area: ['60%', '60%'],
			success: function(layero, index){
			}
		});
		// 注意一定要加 将页面展示
		return false;
	}).mouseenter(function() {
		dialog.tips('处理', '.dealBtn2');
	});

	$('.dealBtn3').click(function () {
		var checkStatus = table.checkStatus('test3'),data = checkStatus.data;
		if(data.length<1){
			layer.msg("请选择一条记录")
			return false;
		}
		if(data.length>1){
			layer.msg("只能选择一条记录")
			return false;
		}
		var url=$(this).attr('data-url')+'?taskId='+data[0].taskId;
		layer.open({
			type: 2,
			title:'处理记录',
			content: url,
			area: ['60%', '60%'],
			success: function(layero, index){
			}
		});
		// 注意一定要加 将页面展示
		return false;
	}).mouseenter(function() {
		dialog.tips('处理', '.dealBtn3');
	});

	//顶部批量删除
	$('.delBtn').click(function() {
        var checkStatus = table.checkStatus('test'),data = checkStatus.data;
        if(data.length<1){
            layer.msg("请选择一条记录")
            return false;
        }
        if(data.length>1){
            layer.msg("只能选择一条记录")
            return false;
        }
		var id = data[0].id;
		var url=$(this).attr('data-url');
		var data = {
			id:id
		}
		dialog.confirm({
			message:'您确定要删除选中项',
			success:function(){
				$.ajax({
					url:url,
					data:data,
					type: 'post',
					dataType:'json',
					success:function (data){
						//let result = JSON.stringify(data);
						if(data.code == 200){
							layer.msg("删除成功");
							table.reload('test',{page:{curr:1}});
						}
					}
				});
			},
			cancel:function(){
				layer.msg('取消删除')
			}
		})
		return false;

	}).mouseenter(function() {

		dialog.tips('删除', '.delBtn');

	})	
	//列表添加
	$('#table-list').on('click', '.add-btn', function() {
		var url=$(this).attr('data-url');
		//将iframeObj传递给父级窗口
		parent.page("菜单添加", url, iframeObj, w = "700px", h = "620px");
		return false;
	})
	//列表删除
	$('#table-list').on('click', '.del-btn', function() {
		var url=$(this).attr('data-url');
		var id = $(this).attr('data-id');
		dialog.confirm({
			message:'您确定要进行删除吗？',
			success:function(){
				layer.msg('确定了')
			},
			cancel:function(){
				layer.msg('取消了')
			}
		})
		return false;
	})
	//列表跳转
	$('#table-list,.tool-btn').on('click', '.go-btn', function() {
		var url=$(this).attr('data-url');
		var id = $(this).attr('data-id');
		window.location.href=url+"?id="+id;
		return false;
	})
	//编辑栏目
	$('#table-list').on('click', '.edit-btn', function() {
		var That = $(this);
		var id = That.attr('data-id');
		var url=That.attr('data-url');
		//将iframeObj传递给父级窗口
		parent.page("菜单编辑", url + "?id=" + id, iframeObj, w = "700px", h = "620px");
		return false;
	})
});

/**
 * 控制iframe窗口的刷新操作
 */
var iframeObjName;

//父级弹出页面
function page(title, url, obj, w, h) {
	if(title == null || title == '') {
		title = false;
	};
	if(url == null || url == '') {
		url = "404.html";
	};
	if(w == null || w == '') {
		w = '700px';
	};
	if(h == null || h == '') {
		h = '350px';
	};
	iframeObjName = obj;
	//如果手机端，全屏显示
	if(window.innerWidth <= 768) {
		var index = layer.open({
			type: 2,
			title: title,
			area: [320, h],
			fixed: false, //不固定
			content: url
		});
		layer.full(index);
	} else {
		var index = layer.open({
			type: 2,
			title: title,
			area: [w, h],
			fixed: false, //不固定
			content: url
		});
	}
}

/**
 * 刷新子页,关闭弹窗
 */
function refresh() {
	//根据传递的name值，获取子iframe窗口，执行刷新
	if(window.frames[iframeObjName]) {
		window.frames[iframeObjName].location.reload();

	} else {
		window.location.reload();
	}

	layer.closeAll();
}